package com.groupsix.controller;

import com.groupsix.entity.Class;
import com.groupsix.service.ClassService;
import com.groupsix.utils.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author 董盛昌
 * @Title: ${file_name}
 * @Package ${package_name}
 * @Description: 班级控制器
 * @date 2021/10/17 16:37
 */
@Controller
public class ClassController {
    // 依赖注入
    @Autowired
    private ClassService classService;

    /**
     * 分页查询
     * pageIndex 当前页码
     * pageSize  显示条数
     */
    @RequestMapping("/findClass.action")
    public String findClass(Integer cClassid, String cClassname, String cCounsellor,
                            Integer pageIndex, Integer pageSize, Model model) {

        PageInfo ci = classService.findPageInfo(cClassname,cCounsellor,
                cClassid,pageIndex,pageSize);
        model.addAttribute("ci",ci);
        model.addAttribute("cClassid",cClassid);
        return "class_list";
    }



    /**
     * 删除学生信息
     */
    @RequestMapping( "/deleteClass.action")
    @ResponseBody
    public String deleteClass(Integer cId) {
        int c = classService.deleteClass(cId);
        return "class_list";
    }

    /**
     * 添加班级信息
     */
    @RequestMapping("/addClass.action")
    @ResponseBody
    public String addClass( @RequestBody Class uclass) {
        int c = classService.addClass(uclass);
        return "class_list";
    }

    @RequestMapping( "/findClassById.action")
    public String findClassById(Integer cId, HttpSession session) {
        Class c= classService.findClassById(cId);
        session.setAttribute("c",c);
        return "class_edit";
    }

    /**
     * 修改班级信息
     */
    @RequestMapping("/updateClass.action")

    public String updateClass( Class uclass) {
        int c = classService.updateClass(uclass);
        return "redirect:/findClass.action";
    }

    /**
     * 班级人员信息查询
     */
    @RequestMapping("/findClassStudent.action")
    public String findClassStudent(Class uclass,Model model) {
        List<Class> c = classService.findClassStudent(uclass);
        model.addAttribute("cs",c);
        return "class_studentlist";
    }
}
